﻿using JetBrains.ReSharper.Daemon.CSharp.Stages;
using JetBrains.ReSharper.Daemon.Stages;
using JetBrains.ReSharper.Daemon.Stages.Dispatcher;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.Psi.CSharp.Tree;
using MugenMvvmToolkit.ReSharper.Highlighting;
using MugenMvvmToolkit.ReSharper.Infrastructure;

namespace MugenMvvmToolkit.ReSharper.ViewModels.Analyzers
{
    [ElementProblemAnalyzer(new[] { typeof(IConstructorDeclaration) },
        HighlightingTypes = new[] { typeof(ShouldBeInitializedHighlighting) })]
    public class ShouldBeInitializedAttributeAnalyzer : ElementProblemAnalyzerBase<IConstructorDeclaration>
    {
        #region Overrides of ElementProblemAnalyzerBase<IConstructorDeclaration>

        protected override void Run(IConstructorDeclaration element, ElementProblemAnalyzerData data,
            IHighlightingConsumer consumer)
        {
            element.ProcessDescendantsForResolve(new OneActionProcessor(node =>
            {
                var expression = node as IReferenceExpression;
                if (expression == null || expression.Parent is IAssignmentExpression ||
                    expression.FindParent<ILambdaExpression>() != null)
                    return;
                if (ToolkitExtensions.HasShouldBeInitializedAttribute(expression))
                    consumer.AddHighlighting(new ShouldBeInitializedHighlighting(node));
            }, node => true));
        }

        #endregion
    }
}